#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author: Florian Dorn <florian.dorn@gmail.com>
# @package: agenda
# @license: GNU GPLv2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import gobject
import gtk
import gtk.gdk

import agenda

from gettext import gettext as _


class SearchAddBar(gtk.Entry):

	TYPE_FILTER = 1
	TYPE_ADD = 2

	__gsignals__ = {
		'filter-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,[gobject.TYPE_STRING]),
		'add-event': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
		                 []),
		'type-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,[gobject.TYPE_INT]),
	}
	__gproperties__ = {
		'type': ( gobject.TYPE_UINT, "Type", "Type", 0, TYPE_ADD, 0, gobject.PARAM_READWRITE ),
	}
	def __init__(self):
		gtk.Entry.__init__(self)

		self.find_image = gtk.Image()
		self.find_image.set_from_stock(gtk.STOCK_FIND,gtk.ICON_SIZE_MENU)
		self.add_image = gtk.Image()
		self.add_image.set_from_stock(gtk.STOCK_ADD,gtk.ICON_SIZE_MENU)

		self.ok_image = gtk.Image()
		self.ok_image.set_from_stock(gtk.STOCK_OK,gtk.ICON_SIZE_BUTTON)
		
		self._attributes = {'type': SearchAddBar.TYPE_FILTER}

		#self.connect('icon-pressed',self.sexy_searchbox_pressed)
		self.connect("key-press-event", self.on_filterEntry_key_pressed)
		self.connect("changed",self.on_change)
		
		#menu_xml = """
		#<popup name="TypeMenu" action="JustifyMenuAction">
		#  <menuitem name="Search" action="search"/>
		#  <menuitem name="Create" action="add"/>
		#</popup>
		#"""
		#manager = gtk.UIManager()
		#manager.add_ui_from_string(menu_xml)
		#action_group = gtk.ActionGroup("name")
		#actions = [	('search', gtk.STOCK_FIND,None,None,None,self.menu_cb),
		#			('add', gtk.STOCK_ADD,None,None,None,self.menu_cb)]
		#action_group.add_actions(actions)
		#manager.insert_action_group(action_group, 0)

		#self.menu = manager.get_widget("/TypeMenu")
		
		#self.set_icon_highlight(sexy.ICON_ENTRY_PRIMARY, True)
		#self.menu.show()
		#self.set_type(SearchAddBar.TYPE_FILTER)
		#self.set_icon_from_pixbuf (gtk.ENTRY_ICON_PRIMARY, self.find_image)
		self.set_icon_from_stock (gtk.ENTRY_ICON_PRIMARY, gtk.STOCK_FIND)
		#self.menu = None
	def on_change(self,w):
		self.emit("filter-changed",self.get_text())
	def menu_cb(self,action = None):
		#print action.get_property("stock-id")
		#print "action: %s" % action
		if(action == None):
			type_ = SearchAddBar.TYPE_FILTER
		else:
			if action.get_name() == "search":
				type_ = SearchAddBar.TYPE_FILTER
			else:
				type_ = SearchAddBar.TYPE_ADD
		self.set_type(type_)
	def get_type(self):
		return self.get_property('type')

	def on_filterEntry_key_pressed(self,  filterEntry, data):
		keyname = gtk.gdk.keyval_name(data.keyval)
		if keyname == "Escape":
			self.set_text("")
			self.emit("filter-changed",self.get_text())
		elif keyname == "Return":
			self.emit("add-event")
			return

	def do_get_property(self, pspec):
		if pspec.name in ('type'):
			return self._attributes[pspec.name]
	def do_set_property(self, pspec, value):
		if self._attributes[pspec.name] == value:
			return

		if pspec.name == 'type':
			self._attributes[pspec.name] = int(value)
			self.emit('type-changed',value)

gobject.type_register(SearchAddBar)
